import 'package:flutter/material.dart';
import 'package:flutter_app_demo/podo/redux_demo_model.dart';
import 'package:flutter_app_demo/redux_demo/actions.dart';
import 'package:flutter_app_demo/redux_demo/widget/Item_list_widget.dart';
import 'package:flutter_app_demo/redux_demo/widget/add_Item_widget.dart';
import 'package:flutter_app_demo/redux_demo/widget/remove_all_Item_widget.dart';
import 'package:flutter_redux/flutter_redux.dart';

import 'reducers.dart';

import 'package:redux/redux.dart';

class ReDuxDemo extends MaterialPageRoute<Null> {
    
    ReDuxDemo()
        : super(builder: (BuildContext ctx) {
        return ReDuxPage();
    });
}

///
/// 注意： 此Store必须处于全局域下，否则控件每次build都会new个新的，导致action操作失效
///
/// <------ourStore在这里初始化，增删改查操作可用
///
final Store<AppState> ourStore = Store<AppState>( //注意Store类来自 package:redux/redux.dart
    appStateReducer,
    initialState: AppState.initState(),
);

class ReDuxPage extends StatelessWidget {
    
    ///<------ourStore在这里初始化会导致action操作失效
    
    @override
    Widget build(BuildContext context) {
        
        ///<------ourStore在这里初始化会导致action操作失效
        
        return Scaffold(
            appBar: AppBar(
                title: Text("Redux Demo"),
            ),
            body: StoreProvider<AppState>( //注意StoreProvider类来自 'package:flutter_redux/flutter_redux.dart';
                store: ourStore,
                child: ReDuxPageBody(),
            ),
        );
    }
}

class ReDuxPageBody extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
        return StoreConnector<AppState, ViewModel>(
            converter: (Store<AppState> store) => ViewModel.create(store),
            builder: (BuildContext context, ViewModel viewModel) {
                return Column(
                    children: <Widget>[
                        
                        AddItemWidget(viewModel),
                        
                        Expanded(
                            child: ItemListWidget(viewModel),
                        ),
                        
                        RemoveAllItemWidget(viewModel),
                    
                    ],
                );
            },
        );
    }
}

class ViewModel {
    final List<Item> items;
    final Function(String) onAddItem;
    final Function(Item) onRemoveItem;
    final Function() onRemoveItems;
    
    ViewModel({
        this.items,
        this.onAddItem,
        this.onRemoveItem,
        this.onRemoveItems,
    });
    
    factory ViewModel.create(Store<AppState> store){
        _onAddItem(String body) {
            store.dispatch(AddItemAction(body));
        }
        _onRemoveItem(Item item) {
            store.dispatch(RemoveItemAction(item));
        }
        _onRemoveItems() {
            store.dispatch(RemoveItemsAction());
        }
        
        return ViewModel(
            items: store.state.items,
            onAddItem: _onAddItem,
            onRemoveItem: _onRemoveItem,
            onRemoveItems: _onRemoveItems,
        );
    }
}